const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    diqu: ['请选择地区'],
    yongrendanwei: ['请选择用人单位'],
    zhaolujiguan: ['请选择招录机关'],
    zhiweimingcheng: ['请选择职位名称'],
  },
  

  onLoad: function (options) {

    let _ = this;
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=45129&callback=?', //接收url
      data: {
        level: '1',
        diqu: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择地区')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].diqu)
        }
        _.setData({
          diqu: list
        });
      }
    })

  },
  bindPickerChange: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('diqu发送选择改变，携带值为', _.data.diqu[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=45129&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'diqu',
        grtext: _.data.diqu[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择用人单位')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].yongrendanwei)
        }
        _.setData({
          yongrendanwei: list
        });
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let _ = this;
    console.log('yongrendanwei发送选择改变，携带值为', _.data.yongrendanwei[num2]);
    this.setData({
      index2: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=45129&callback=?', //接收url
      data: {
        level: '3',
        grfiled: 'yongrendanwei',
        grtext: _.data.yongrendanwei[num2],
        onefiled: 'diqu',
        onetext: _.data.diqu[this.data.index]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择招录机关')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].zhaolujiguan)

        }
        _.setData({
          zhaolujiguan: list
        });
      }
    })
  },
  bindPickerChange3: function (e) {
    let num3 = e.detail.value;
    let _ = this;
    console.log('zhaolujiguan发送选择改变，携带值为', _.data.zhaolujiguan[num3]);
    this.setData({
      index3: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=45129&callback=?', //接收url
      data: {
        level: '4',
        grfiled: 'zhaolujiguan',
        grtext: _.data.zhaolujiguan[num3],
        onefiled: 'diqu',
        onetext: _.data.diqu[this.data.index],
        twofiled: 'yongrendanwei',
        twotext: _.data.yongrendanwei[this.data.index2],
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson.lists)
        var list = [];
        list.push('请选择职位名称')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].zhiweimingcheng)
        }
        _.setData({
          zhiweimingcheng: list
        });
      }
    })
  },
  bindPickerChange4: function (e) {
    let num4 = e.detail.value;
    let _ = this;
    console.log('zhiweimingcheng发送选择改变，携带值为', _.data.zhiweimingcheng[num4]);
    this.setData({
      index4: e.detail.value
    })
  },
  tijiao: function () {
    var _ = this;
    var diqu = _.data.diqu[_.data.index];
    var yongrendanwei = _.data.yongrendanwei[_.data.index2];
    var zhaolujiguan = _.data.zhaolujiguan[_.data.index3];
    var zhiweimingcheng = _.data.zhiweimingcheng[_.data.index4];
    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/shaifen/getfzinfo/?actid=45129&callback=?",
      data: {
        diqu: diqu,
        yongrendanwei: yongrendanwei,
        zhaolujiguan: zhaolujiguan,
        zhiweimingcheng: zhiweimingcheng,
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson)
        if(theJson.status == 1){
          _.setData({
            theln: theJson.lists,
            ij:1,
            ji:0
          })
        } else {
          _.setData({
            theln: '该职位代码未查询到有效信息',
            ij:0,
            ji:1
          })
        }
        
      }
    })
  },

  toShoulei: function () {
    wx.navigateTo({
      url: '../shoulei/shoulei',
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})